package com.example.wssapp;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Window;

import com.example.wssapp.fragment.Fragment1;
import com.example.wssapp.fragment.Fragment1.Fragment1Listener;
import com.example.wssapp.fragment.Fragment2;

public class ContractsActivity extends Activity implements Fragment1Listener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.frag_contracts);
		
		initFragment1();
	}

	private void initFragment1() {
		Fragment1 f1 = new Fragment1();
		FragmentTransaction ft = getFragmentManager().beginTransaction();
		ft.replace(R.id.fl1, f1);
		ft.commit();
	}

	@Override
	public void onClickButton() {
		FragmentTransaction ft = getFragmentManager().beginTransaction();
		
		if (findViewById(R.id.fl2) != null) {
			Fragment2 fragment2 = (Fragment2) getFragmentManager()
					.findFragmentById(R.id.fl2);
			if (fragment2 == null) {
				fragment2 = new Fragment2();
				ft.replace(R.id.fl2, fragment2);
				ft.commit();
			}
		} else {
			Fragment2 fragment2 = new Fragment2();
			ft.replace(R.id.fl1, fragment2);
			ft.commit();
		}
	}
	
	protected void initFragment2(Fragment fragment, int id) {
		FragmentTransaction ft = getFragmentManager().beginTransaction();
		ft.replace(id, fragment);
		ft.commit();
	}
	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		Fragment2 fragment2 = (Fragment2) getFragmentManager()
				.findFragmentById(R.id.fl2);
		if(fragment2 != null && fragment2.isVisible()) {
			if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
				// 横屏
			} else if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
				// 竖屏
				// initFragment2(fragment2, R.id.fl1);
			}
		}
	}
	
}
